package Model;
/**
 * @author Hugo
 */

import Controller.ReservationController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

public final class Reservation extends AbstractModel{
    private int _numConfirmation;
    private Client _client;
    private LinkedList<LigneReservation> _ligneReservation;
    
    //**Constructeurs**//
    public Reservation(){
        setNumConfirmation();
    }
    
    public void initDefault() {
        setClient(new Client());
        _ligneReservation = new LinkedList<>();
    }
        
    //**Accesseurs**//
    public int getNumConfirmation(){
        return _numConfirmation;
    }
    
    public Client getClient(){
        return _client;
    }
    
    public LinkedList<LigneReservation> getligneReservation(){
        return _ligneReservation;
    }
    
    //**Mutateurs**//
    public void setNumConfirmation(){
        int oldNumConfirmation = this._numConfirmation;
        
        _numConfirmation = Agenda.getInstance().getNextID();
        
        firePropertyChange(ReservationController.NUM_CONFIRMATION_PROPERTY, oldNumConfirmation, _numConfirmation);
    }
    
    public void setClient(Client Client){
        
        Client oldClient = this._client;
        _client = Client;
        modifyReservationInAgenda();
        
        firePropertyChange(ReservationController.CLIENT_PROPERTY, oldClient, _client);
    }
    
    public void setNomClient(String nomClient){
        
        Client oldClient = this._client;
        _client.setNomClient(nomClient);
        modifyReservationInAgenda();
        
        firePropertyChange(ReservationController.CLIENT_NOM_PROPERTY, oldClient, _client);
    }
    
    public void setNumClient(String numclient){
        
        Client oldClient = this._client;
        
        _client.setNumTel(numclient);
        modifyReservationInAgenda();
        
        firePropertyChange(ReservationController.CLIENT_NUM_PROPERTY, oldClient, _client);
    }
    
    public void updateReservation(){
        Agenda.getInstance().addReservation(this);
    }
    
    public void deleteReservation(){
        Agenda.getInstance().deleteReservation(this);
    }
    
    public void addLigneReservation(LigneReservation ligneReservation){
        LinkedList<LigneReservation> oldLigneReservation = _ligneReservation;
        
        if(Agenda.getInstance().verifierDisponibilite(ligneReservation)){

            _ligneReservation.add(ligneReservation);
            modifyReservationInAgenda();
            
            String[][] data = create2DString();
            firePropertyChange(ReservationController.LIGNE_RESERVATION_PROPERTY, oldLigneReservation, data);
        }else{
            String[][] data = create2DString();
            firePropertyChange(ReservationController.NOT_ENOUGH_PLACE_EVENT, oldLigneReservation, data);
        }
        
    }
    
    public void updateLigneReservation(){
        LinkedList<LigneReservation> oldLigneReservation = _ligneReservation;
        String[][] data = create2DString();
        firePropertyChange(ReservationController.LIGNE_RESERVATION_PROPERTY, oldLigneReservation, data);
    }
    
    public void updateNumConfirmation(){
        int oldNumConfirmation = 0;
        firePropertyChange(ReservationController.NUM_CONFIRMATION_PROPERTY, oldNumConfirmation, _numConfirmation);
    }
    
    private String[][] create2DString(){
        LinkedList<LigneReservation> reservations = _ligneReservation;
        String[][] data = new String[reservations.size()][4];
        for(int i=0; i < reservations.size(); i++)
        {
            data[i][0] = String.valueOf(reservations.get(i).getNombreChambre());
            data[i][1] = reservations.get(i).getCat().getNomCategorie();
            data[i][2] = DateToString(reservations.get(i).getDteArrive());
            data[i][3] = DateToString(reservations.get(i).getDteFin());
        }
        
        return data;
    }
    
    private String DateToString(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        
        StringBuilder stringDate = new StringBuilder(dateFormat.format(date));
        
        return stringDate.toString();
    }
    
    private void modifyReservationInAgenda(){
        Agenda.getInstance().overwriteReservation(this);
    }
}
